package com.hht.downloaddemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.hht.downloadmanager.call.UpdateUICall;
import com.hht.downloadmanager.down.DownLoadManager;
import com.hht.downloadmanager.utils.MmkvUtils;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    public static final String URL_OTA = "https://oss.igrsservice.com/upload/dps/update_T55_1.0.6.zip";
    public static final String URL_QQ = "https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk";
    private TextView mTextView;
    private Button onCancelView, onStartView, onPauseView;
    private ProgressBar progressBar;
    private DownLoadManager downLoadTools;
    private static final String KEY_COUNT = "key_count";
    private static final String KEY_LENGTH = "key_length";
    private static final String KEY_PROGRESS = "key_progress";

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.text_view);
        progressBar = findViewById(R.id.progress_bar);
        onPauseView = findViewById(R.id.pause_tv);
        onStartView = findViewById(R.id.start_tv);
        onCancelView = findViewById(R.id.cancel_tv);

        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            downLoadTools = new DownLoadManager.Builder().setFileUrl(URL_OTA).
                    setThreadNumber(5).
                    build(this);
//            downLoadTools.onStart();
            downLoadTools.setUpdateUICall(new UpdateUICall() {
                @Override
                public void onProgress(long fileCountLength, long downFileLength, int progress, String downSpeed) {

                    if (MmkvUtils.getInstance(MainActivity.this).getLongData(KEY_LENGTH) > 0) {

                    }
                    mTextView.setText("总大小为：" + fileCountLength / 1024 / 1024 + "MB, 当前下载大小为：" + downFileLength / 1024 / 1024 + "MB,下载速度为：" + downSpeed);
                    progressBar.setProgress(progress);
                }

                @Override
                public void onDownLoadFinished(File downFile) {
                    MmkvUtils.getInstance(MainActivity.this).clearData(KEY_COUNT);
                    MmkvUtils.getInstance(MainActivity.this).clearData(KEY_LENGTH);
                    MmkvUtils.getInstance(MainActivity.this).clearData(KEY_PROGRESS);
                    mTextView.setText("下载完成，总大小为：" + downFile.length() / 1024 / 1024 + "MB 文件路径为：" + downFile.getAbsolutePath());
                    progressBar.setProgress(100);
                }

                @Override
                public void onDownLoadPause(long fileCountLength, long downFileLength, int progress) {
                    // 此处为保存进度，关机后能显示UI
                    MmkvUtils.getInstance(MainActivity.this).save(KEY_COUNT, fileCountLength);
                    MmkvUtils.getInstance(MainActivity.this).save(KEY_LENGTH, downFileLength);
                    MmkvUtils.getInstance(MainActivity.this).save(KEY_PROGRESS, progress);

                }

                @Override
                public void onDownLoadCancel() {
                    MmkvUtils.getInstance(MainActivity.this).clearData(KEY_COUNT);
                    MmkvUtils.getInstance(MainActivity.this).clearData(KEY_LENGTH);
                    MmkvUtils.getInstance(MainActivity.this).clearData(KEY_PROGRESS);
                }
            });
        }

        onPauseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downLoadTools.onPause();
            }
        });

        onStartView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downLoadTools.onStart();
            }
        });

        onCancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downLoadTools.onCancel();
            }
        });
        mTextView.setText("总大小为：" + MmkvUtils.getInstance(MainActivity.this).getLongData(KEY_COUNT) / 1024 / 1024
                + "MB, 当前下载大小为：" + MmkvUtils.getInstance(MainActivity.this).getLongData(KEY_LENGTH) / 1024 / 1024 + "MB,下载速度为："
                + 0);

        progressBar.setProgress(MmkvUtils.getInstance(MainActivity.this).getIntegerData(KEY_PROGRESS));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        downLoadTools.onPause();
    }
}